package com.wtwd.campus.jdzc.service.impl;

import com.netflix.discovery.converters.Auto;
import com.wtwd.campus.entity.DeviceInfo;
import com.wtwd.campus.jdzc.dao.JdzcUserStudentPermissionMapper;
import com.wtwd.campus.jdzc.entity.JdzcUserStudentPermission;
import com.wtwd.campus.jdzc.service.ValidService;
import com.wtwd.campus.service.RedisService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: ldaoliang
 * @DateTime: 2022/2/14 16:11
 * @Description:
 */
@Service("jdzcValidService")
public class ValidServiceImpl implements ValidService {

    @Autowired
    private RedisService redisService;
    @Autowired
    private JdzcUserStudentPermissionMapper jdzcUserStudentPermissionMapper;

    @Override
    public DeviceInfo validDevice(Integer deviceId) {
        return null;
    }

    @Override
    public DeviceInfo validStudent(Integer studentId, String token) {
        return null;
    }

    @Override
    public boolean updateOperation(DeviceInfo deviceInfo, String token) {
        return false;
    }

    @Override
    public boolean validStudentAndTokenCorrespond(Integer studentId, String token) {
        if(studentId == null || StringUtils.isBlank(token)){
            return false;
        }
        // 1.从token中获取用户id
        String userIdString = redisService.getValue(token);
        if(StringUtils.isBlank(userIdString)){
            return false;
        }
        Integer userId = Integer.parseInt(userIdString);
        JdzcUserStudentPermission permission = jdzcUserStudentPermissionMapper.getWxUserStudentPermissionByCondition(studentId, userId);
        return !(permission == null);
    }

    @Override
    public int validCanOperation(Integer studentId, String token) {
        if(studentId == null || StringUtils.isBlank(token)){
            return 0;
        }
        // 1.从token中获取用户id
        String userIdString = redisService.getValue(token);
        if(StringUtils.isBlank(userIdString)){
            return 0;
        }
        Integer userId = Integer.parseInt(userIdString);
        JdzcUserStudentPermission permission = jdzcUserStudentPermissionMapper.getWxUserStudentPermissionByCondition(studentId, userId);
        if(permission == null){
            return 0;
        }
        return permission.getIsAuthorization() == 1 ? 2 : 1;
    }
}
